{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change directory to VSCode workspace root so that relative path loads work correctly. Turn this addition off with the DataScience.changeDirOnImportExport setting\r\n",
    "import os\r\n",
    "try:\r\n",
    "\tos.chdir(os.path.join(os.getcwd(), 'github\\ToBeQuanter'))\r\n",
    "\tprint(os.getcwd())\r\n",
    "except:\r\n",
    "\tpass\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA\n",
    "import datetime\n",
    "\n",
    "account = QA.QA_Account(\n",
    "    user_cookie='tts',\n",
    "    portfolio_cookie='tts',\n",
    "    account_cookie='tts',\n",
    "    broker=QA.BROKER_TYPE.TTS, \n",
    "    running_environment=QA.RUNNING_ENVIRONMENT.TTS\n",
    ")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "subscribe\n[<_MainThread(MainThread, started 17540)>, <Thread(Thread-4, started daemon 12988)>, <Heartbeat(Thread-5, started daemon 22424)>, <HistorySavingThread(IPythonHistorySavingThread, started 19040)>, <ParentPollerWindows(Thread-3, started daemon 8928)>, <Thread(pymongo_server_monitor_thread, started daemon 22608)>, <Thread(pymongo_kill_cursors_thread, started daemon 3956)>, <Thread(pymongo_kill_cursors_thread, started daemon 5396)>,  <QA_ENGINE with ['ORDER'] kernels ident 21192>, <QA_Thread: ORDER  id=2496805951640 ident 19328>]\n"
     ]
    }
   ],
   "source": [
    "market = QA.QA_Market(if_start_orderthreading=True)\n",
    "market.connect(QA.BROKER_TYPE.TTS)\n",
    "market.login(QA.BROKER_TYPE.TTS, account_cookie=account.account_cookie, account=account)\n",
    "market.start()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "184707.03\n"
     ]
    }
   ],
   "source": [
    "print(market.query_cash(account.account_cookie))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'cash': 184707.03, 'hold': {'510050': 0.0, '510300': 0.0, '600612': 1200.0, '600886': 0.0, '002697': 5400.0}}\n"
     ]
    }
   ],
   "source": [
    "print(market.query_assets(account.account_cookie))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                       order_time    code    name  towards  trade_price  \\\nrealorder_id                                                              \n11378         2019-04-02 10:12:55  002697    红旗连锁        1        0.000   \n12246         2019-04-02 10:23:13  510050   50ETF       -1        0.000   \n12307         2019-04-02 10:23:44  510300  300ETF       -1        0.000   \n12359         2019-04-02 10:24:21  002697    红旗连锁        1        0.000   \n12384         2019-04-02 10:24:39  600886    国投电力       -1        8.430   \n1256          2019-04-02 09:22:44  600886    国投电力       -1        0.000   \n1316          2019-04-02 09:23:40  510300  300ETF       -1        0.000   \n1362          2019-04-02 09:24:03  510300  300ETF       -1        0.000   \n13962         2019-04-02 10:48:20  510050   50ETF       -1        0.000   \n14011         2019-04-02 10:49:10  510300  300ETF       -1        3.956   \n14019         2019-04-02 10:49:17  510050   50ETF       -1        2.863   \n1430          2019-04-02 09:24:32  510050   50ETF       -1        0.000   \n1479          2019-04-02 09:24:54  510050   50ETF       -1        0.000   \n1613          2019-04-02 09:26:05  002697    红旗连锁        1        5.800   \n1730          2019-04-02 09:27:12  002697    红旗连锁        1        5.740   \n1791          2019-04-02 09:27:40  002697    红旗连锁        1        0.000   \n24310         2019-04-02 14:53:22  002697    红旗连锁        1        0.000   \n2791          2019-04-02 09:32:27  002697    红旗连锁        1        5.790   \n\n              order_price       status  order_amount  trade_amount  \\\nrealorder_id                                                         \n11378               5.680   cancel_all        2700.0           0.0   \n12246               2.889   cancel_all        7200.0           0.0   \n12307               3.988   cancel_all        5400.0           0.0   \n12359               5.680   cancel_all        2700.0           0.0   \n12384               8.430  success_all        2400.0        2400.0   \n1256                8.460   cancel_all        2400.0           0.0   \n1316                3.996   cancel_all        2700.0           0.0   \n1362                4.020   cancel_all        2700.0           0.0   \n13962               2.869   cancel_all        7200.0           0.0   \n14011               1.000  success_all        5400.0        5400.0   \n14019               1.000  success_all        7200.0        7200.0   \n1430                2.893   cancel_all        3600.0           0.0   \n1479                2.912   cancel_all        3600.0           0.0   \n1613                5.800  success_all        1800.0        1800.0   \n1730                5.740  success_all        1800.0        1800.0   \n1791                5.680   cancel_all        1800.0           0.0   \n24310               5.720       queued        2700.0           0.0   \n2791                5.800  success_all        1800.0        1800.0   \n\n              cancel_amount  \nrealorder_id                 \n11378                     0  \n12246                     0  \n12307                     0  \n12359                     0  \n12384                     0  \n1256                      0  \n1316                      0  \n1362                      0  \n13962                     0  \n14011                     0  \n14019                     0  \n1430                      0  \n1479                      0  \n1613                      0  \n1730                      0  \n1791                      0  \n24310                     0  \n2791                      0  \n"
     ]
    }
   ],
   "source": [
    "print(market.query_orders(account.account_cookie))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "order_time       2019-04-02 10:24:39\ncode                          600886\nname                            国投电力\ntowards                           -1\ntrade_price                     8.43\norder_price                     8.43\nstatus                   success_all\norder_amount                    2400\ntrade_amount                    2400\ncancel_amount                      0\nName: (tts, 12384), dtype: object\n"
     ]
    }
   ],
   "source": [
    "print(market.query_order(account.account_cookie, '12384'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "code\n002697    5400.0\n600612    1200.0\nName: amount, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(market.query_position(account.account_cookie))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ">-----------------------insert_order-----------------------------> QA_Market.insert_order\n100 by_amount 2019-04-02 16:35:22.004487 600825 1 LIMIT 1 None\nFAILED FOR CREATE ORDER tts failed\n< QA_Order realorder_id Order_CItwbsoq datetime:2019-04-02 09:31:00 code:600825 amount:100 price:1 towards:1 btype:stock_cn order_id:Order_CItwbsoq account:tts status:failed >\nfailed\n"
     ]
    }
   ],
   "source": [
    "market.insert_order(account_cookie=account.account_cookie, code='600825', amount=100, amount_model=QA.AMOUNT_MODEL.BY_AMOUNT, order_model=QA.ORDER_MODEL.LIMIT,\n",
    "                           frequence=QA.FREQUENCE.CURRENT, broker_name=account.broker, market_type=account.market_type,\n",
    "                           towards=QA.ORDER_DIRECTION.BUY, price=1, time=datetime.datetime.now())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(market.query_trade(account.account_cookie))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   }
  },
  "orig_nbformat": 2.0,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3.0
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
